/**
 * 
 */
package com.security.frame.core.code;

import com.security.frame.core.enums.ValidateCodeType;
import com.security.frame.core.exception.ValidateCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 查找不同验证码生成器
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/11/6
 */
@Component
@Slf4j
public class ValidateCodeProcessorHolder {

	@Autowired
	private Map<String, ValidateCodeProcessor> validateCodeProcessors;

	public ValidateCodeProcessor findValidateCodeProcessor(ValidateCodeType type) {
		return findValidateCodeProcessor(type.toString().toLowerCase());
	}

	public ValidateCodeProcessor findValidateCodeProcessor(String type) {
		String name = type.toLowerCase() + ValidateCodeProcessor.class.getSimpleName();
		ValidateCodeProcessor processor = validateCodeProcessors.get(name);
		if (processor == null) {
			log.error("【验证码处理器{}不存在】",name);
			throw new ValidateCodeException("验证码处理器" + name + "不存在");
		}
		return processor;
	}

}
